﻿using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.CSharp;

namespace TaskGenerater
{
    public static class Helper
    {
        public static List<string> SerachFile(string fileDirectory, string fileName)
        {
            var dir = new DirectoryInfo(fileDirectory);
            var children = dir.GetFileSystemInfos();
            var result = new List<string>();
            foreach (var child in children)
            {
                if (child is FileInfo && child.Name == fileName)
                {
                    result.Add(child.FullName);
                    continue;
                }
                if (child is DirectoryInfo)
                    result.AddRange(SerachFile(child.FullName, fileName));
            }
            return result;
        }

        public static Assembly CompileToAssembly(string file)
        {
            var privoder = new CSharpCodeProvider();
            var parameters = new CompilerParameters();
            parameters.ReferencedAssemblies.Add("System.dll");
            parameters.ReferencedAssemblies.Add("System.Runtime.Serialization.dll");
            parameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;
            return privoder.CompileAssemblyFromFile(parameters, file).CompiledAssembly;
        }
    }
}